//
//  CircleContentVO.m
//  GolfFriend
//
//  Created by Vescky on 13-11-26.
//  Copyright (c) 2013年 vescky.org. All rights reserved.
//

#import "CircleContentVO.h"
#import <objc/runtime.h>

@implementation CircleContentVO

@synthesize articleId,content,userId,time,sourceLinks,userName,cityName,userNickName,avatar;
@synthesize isSupported;

- (id)init{
    self = [super init];
    
    return self;
}

- (id)copyWithZone:(NSZone *)zone {
    id copy = [[[self class] alloc] init];
    if (copy) {
        [copy setArticleId:self.articleId];
        [copy setContent:self.content];
        [copy setUserId:self.userId];
        [copy setUserName:self.userName];
        [copy setCityName:self.cityName];
        [copy setTime:self.time];
        [copy setSourceLinks:self.sourceLinks];
        [copy setClicks:self.clicks];
        [copy setComments:self.comments];
        [copy setUserNickName:self.userNickName];
        [copy setAvatar:self.avatar];
        [copy setZan:self.zan];
        [copy setIsSupported:self.isSupported];
    }
    return copy;
}

- (NSDictionary *)toDictionary
{
    NSMutableDictionary *dictionaryFormat = [NSMutableDictionary dictionary];
    //  取得当前类类型
    Class cls = [self class];
    
    unsigned int ivarsCnt = 0;
    //　获取类成员变量列表，ivarsCnt为类成员数量
    Ivar *ivars = class_copyIvarList(cls, &ivarsCnt);
    
    //　遍历成员变量列表，其中每个变量都是Ivar类型的结构体
    for (const Ivar *p = ivars; p < ivars + ivarsCnt; ++p)
    {
        Ivar const ivar = *p;
        //　获取变量名
        NSString *key = [NSString stringWithUTF8String:ivar_getName(ivar)];
        // 若此变量未在类结构体中声明而只声明为Property，则变量名加前缀 '_'下划线
        // 比如 @property(retain) NSString *abc;则 key == _abc;
        //　获取变量值
        id value = [self valueForKey:key];
        //　取得变量类型
        // 通过 type[0]可以判断其具体的内置类型
        //        const char *type = ivar_getTypeEncoding(ivar);
        if (value)
        {
            [dictionaryFormat setObject:value forKey:key];
        }
    }
    
    return dictionaryFormat;
    
}
- (void)dictionaryToCircleContentVO:(NSDictionary*)cDict {
    [self setArticleId:[cDict objectForKey:@"articleId"]];
    [self setContent:[cDict objectForKey:@"content"]];
    [self setUserId:[cDict objectForKey:@"userId"]];
    [self setUserName:[cDict objectForKey:@"userName"]];
    [self setCityName:[cDict objectForKey:@"cityName"]];
    [self setTime:[cDict objectForKey:@"time"]];
    [self setSourceLinks:[cDict objectForKey:@"sourceLinks"]];
    [self setClicks:[cDict objectForKey:@"clicks"]];
    [self setZan:[cDict objectForKey:@"zan"]];
    [self setComments:[cDict objectForKey:@"comments"]];
    [self setAvatar:[cDict objectForKey:@"avatar"]];
    [self setUserNickName:[cDict objectForKey:@"userNickName"]];
    [self setIsSupported:[cDict objectForKey:@"isSupported"]];
}

@end
